package com.javastudy.autowiredStudy.springconverter;

import org.apache.commons.lang.StringUtils;
import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * converter<S,R> 的使用场景
 * @author mac
 * @date 2021/9/7 3:28 下午
 */
public class DateConverter implements Converter<String, Date> {
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    public Date convert(String source) {
        Date result = null;
        if (StringUtils.isNotBlank(source)){
            try {
                result = simpleDateFormat.parse(source);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}
